package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.TurnoverYear;
import co.yixiang.modules.system.service.TurnoverYearService;
import co.yixiang.modules.system.dto.queryCriteria.TurnoverYearQueryCriteria;
import co.yixiang.modules.system.dto.TurnoverYearDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author zf
* @date 2020-08-15
*/
@AllArgsConstructor
@Api(tags = "营业额年统计管理")
@RestController
@RequestMapping("/api/turnoverYear")
public class TurnoverYearController {

    private final TurnoverYearService turnoverYearService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','turnoverYear:list')")
    public void download(HttpServletResponse response, TurnoverYearQueryCriteria criteria) throws IOException {
        turnoverYearService.download(Convert.toList(TurnoverYearDto.class, turnoverYearService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询营业额年统计")
    @ApiOperation("查询营业额年统计")
    @PreAuthorize("@el.check('admin','turnoverYear:list')")
    public ResponseEntity<Object> getTurnoverYears(TurnoverYearQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(turnoverYearService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增营业额年统计")
    @ApiOperation("新增营业额年统计")
    @PreAuthorize("@el.check('admin','turnoverYear:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TurnoverYear resources){
        return new ResponseEntity<>(turnoverYearService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改营业额年统计")
    @ApiOperation("修改营业额年统计")
    @PreAuthorize("@el.check('admin','turnoverYear:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TurnoverYear resources){
        turnoverYearService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除营业额年统计")
    @ApiOperation("删除营业额年统计")
    @PreAuthorize("@el.check('admin','turnoverYear:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<String> ids) {
        turnoverYearService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
